 //
//  MPTLiveMeVC.m
//  MPTLive
//
//  Created by HouGeng on 16/1/27.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "MPTLiveMeVC.h"
#import "GuanZhuListVC.h"
#import "MPTLiveMeHeadView.h"
#import "MPTLiveMeCell.h"
#import "MeInfoMainVC.h"
#import "NSString+MPTString.h"
#import "MPTLoginUser.h"
#import "MeSettingMainVC.h"
#import "HeadRefreshView.h"
#import "MPTLiveViewController.h"
#import "LiveVC.h"
#import <UIWindow+MPTWindow.h>

@interface MPTLiveMeVC ()
<
UITableViewDataSource,
UITableViewDelegate
>


@property (nonatomic, strong) UITableView *tableViewMine;

@property (nonatomic, strong) NSDictionary *dicData;

@property (nonatomic, assign) NSInteger intPage;

@property (nonatomic, strong) MPTLiveMeHeadView *viewHead;

@end


@implementation MPTLiveMeVC


#pragma mark - ********************** View Lifecycle **********************

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVariable]; /// 初始化变量
    [self initViews]; /// 初始化界面
    
    [self req_URL_shot_channel]; /// 发送请求
    
    [self regNotification]; /// 注册消息
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[TPTabBarManager share] showBottomView:YES animation:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    // 一定要关注这个函数是否被执行了！！！
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - ********************** Init All Views **********************

- (void)initVariable
{
    _intPage = 1;
}

- (void)initViews
{
    [self initNavView]; /// 初始化Nav导航栏
    
    /// 根据界面情况，决定是单独写函数，或直接写Views
    [self initMainViews];
}

- (void)initNavView
{
    /// 创建导航栏
    [self createNavBarViewWithTitle:@"我的"];
    self.superNavBarView.backgroundColor = RGBAColor(254, 222, 9, 1);
    self.superNavBarView.viewBottomLine.backgroundColor = RGBAColor(254, 222, 9, 1);
}

- (void)initMainViews
{
    // 绑定成功消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MeLiveClicked) name:@"MeLive" object:nil];
    [self initTableView];
}


#pragma mark - ********************** Functions **********************

#pragma mark - 注册消息

- (void)regNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(req_URL_shot_channel) name:@"MPTLIVE_RequestNotification" object:nil];
}

#pragma mark - demo

- (void)initTableView
{
    self.tableViewMine = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                                       TP_NavBar_H,
                                                                       screenW,
                                                                       screenH-TP_NavBar_H-49)
                                                      style:UITableViewStyleGrouped];
    self.tableViewMine.delegate = self;
    self.tableViewMine.dataSource = self;
    [self.tableViewMine setSeparatorColor:RGBAllColor(0xdddddd)];
    [self.view addSubview:self.tableViewMine];
    
    [self.tableViewMine addHeadRefreshViewWithTarget:self action:@selector(req_URL_shot_channel)];
    [self.tableViewMine.YX_HeadRefreshView addCustomView:[HeadRefreshView createHeadRefresh]];
    
    // 临时创建headview
    CGRect rect = CGRectMake(0,
                             0,
                             self.tableViewMine.frame.size.width,
                             100);
    MPTLiveMeHeadView *viewHead = [[MPTLiveMeHeadView alloc] initWithFrame:rect];
    self.tableViewMine.tableHeaderView = viewHead;
    [viewHead setBtnTarget:self withShowInfoAction:@selector(btnClickShowInfo:)];
}


#pragma mark - ********************** Button Touch Event **********************

- (void)btnClickBack:(TPButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnClickShowInfo:(TPButton *)sender
{
    MeInfoMainVC *vc = [[MeInfoMainVC alloc] init];
    vc.dicData = [NSMutableDictionary dictionaryWithDictionary:_dicData];
    
    __weak MPTLiveMeVC *weakSelf = self;
    /// 改变数据的block
    [vc setBlock:^(NSDictionary *dict)
    {
        weakSelf.dicData = dict;
    }];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - ********************** Net Connection Event **********************

#pragma mark - 请求 xxx

- (void)req_URL_shot_channel
{
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSDictionary *paraDic = @{@"os":@"ios",
                              @"page":@(_intPage),
                              @"version":ZB_Str_Protect(version),
                              @"unique_id":ZB_Str_Protect([NSString MPTIdentifierString]),
                              @"showType":@"2",
                              @"suid":ZB_Str_Protect([MPTLoginUser unArchiver].suid)};
    
    [[MPTHttpClient sharedMPHTTPClient] getPath:@"m/shot_channel.json" parameters:paraDic queuePriority:NSOperationQueuePriorityNormal cacheRequest:NO  success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.tableViewMine.YX_HeadRefreshView endRefreshing];
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            if ([responseObject[@"status"] integerValue] == 200)
            {
                self.dicData = [NSDictionary dictionaryWithDictionary:responseObject];
                MPTLiveMeHeadView *viewHead = [[MPTLiveMeHeadView alloc] initWithFrame:CGRectMake(0,
                                             0,
                                             self.tableViewMine.frame.size.width,
                                             [MPTLiveMeHeadView getViewH:self.dicData])];
                viewHead.dicData = self.dicData;
                self.tableViewMine.tableHeaderView = viewHead;
                [viewHead setBtnTarget:self withShowInfoAction:@selector(btnClickShowInfo:)];
            }
            
            [self.tableViewMine reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.tableViewMine.YX_HeadRefreshView endRefreshing];
    } cached:nil];
}


#pragma mark - ********************** Delegate Event **********************

#pragma mark - ********************** Delegate Event **********************


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MPTLiveMeCell getCellH];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 2;
    }
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"MPTLiveMeCellIdentifier";
    MPTLiveMeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[MPTLiveMeCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:cellId];
    }
    
    [cell setDataWithDic:_dicData[@"header"][@"eventCnt"] indexpath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        GuanZhuListVC *vc = [[GuanZhuListVC alloc] init];
        vc.fromeType = GuanZhu;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if(indexPath.section == 0 && indexPath.row == 1)
    {
        GuanZhuListVC *vc = [[GuanZhuListVC alloc] init];
        vc.fromeType = FenShi;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.section == 1 && indexPath.row == 0)
    {
        MeSettingMainVC *mainVC = [[MeSettingMainVC alloc] init];
        [self.navigationController pushViewController:mainVC animated:YES];
    }
}



#pragma mark - ********************** Notification Event **********************

#pragma mark - 通知在个人页面点击了直播

- (void)MeLiveClicked
{
    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:LiveCode];
    
    if(number.integerValue == 1)
    {
        // 具备直播权限
        MPTLiveViewController *mainVC = [[MPTLiveViewController alloc] init];
        [[UIApplication sharedApplication].keyWindow.visibleViewController presentViewController:mainVC animated:YES completion:nil];
    }
    else
    {
        // 不具备直播权限
        LiveVC *vc = [[LiveVC alloc] init];
        [self presentViewController:vc animated:YES completion:nil];
    }
}


@end
